package com.yxzx.ebs.controller;


import com.yxzx.common.YxzxResult;
import com.yxzx.ebs.entity.EbsSubject;
import com.yxzx.ebs.entity.vo.EbsSubjectNestedVo;
import com.yxzx.ebs.service.EbsSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author kongfuzi
 * @since 2020-07-29
 */
@Api(tags = "分类管理")
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/eduservice/subject")
public class EbsSubjectController {
    @Autowired
    private EbsSubjectService subjectService;
    //通过上传excel文件获取文件内容
    @ApiOperation("导入分类")
    @PostMapping("import")
    public YxzxResult importExcelSubject(@ApiParam(name = "file",value = "导入的文件", required = true)
                                             @RequestParam("file") MultipartFile file){
        List<String> list = subjectService.importSubject(file);
        return list.size()>0 ? YxzxResult.error().data("errorMsg",list) : YxzxResult.ok();
    }

    @ApiOperation("获取嵌套数据列表")
    @GetMapping
    public YxzxResult nestedList(){
        List<EbsSubjectNestedVo>  subjectNestedVoList = subjectService.nestedList();
        return YxzxResult.ok().data("items",subjectNestedVoList);
    }

    @ApiOperation("根据ID删除分类")
    @DeleteMapping("{id}")
    public YxzxResult deleteSubjectById(@ApiParam(name = "id",value = "分类主键",required = true)
                                        @PathVariable String id){
        boolean b = subjectService.deleteSubjectById(id);
        return b ? YxzxResult.ok() : YxzxResult.error().message("删除失败");
    }

    @ApiOperation("添加一级分类")
    @PostMapping("saveLevelOne")
    public YxzxResult saveLevelOne(@ApiParam(name = "subject",value = "分类对象", required = true)
                                   @RequestBody EbsSubject subject){
        boolean b = subjectService.saveLevelOne(subject);
        return b ? YxzxResult.ok() : YxzxResult.error().message("新增一级分类失败");
    }

    @ApiOperation("添加二级分类")
    @PostMapping("saveLevelTwo")
    public YxzxResult saveLevelTwo(@ApiParam(name = "subject",value = "分类对象", required = true)
                                   @RequestBody EbsSubject subject){
        boolean b = subjectService.saveLevelTwo(subject);
        return b ? YxzxResult.ok() : YxzxResult.error().message("新增二级分类失败");
    }

}

